package com.sl.hx.shop.ui.mine.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.sl.hx.shop.R;
import com.sl.hx.shop.base.BaseActivity;
import com.sl.hx.shop.bean.ImageUrlBean;
import com.sl.hx.shop.bean.NickName;
import com.sl.hx.shop.constant.AppConfig;
import com.sl.hx.shop.http.ApiFactory;
import com.sl.hx.shop.http.HttpUtil;
import com.sl.hx.shop.http.ProgressSubscribe;
import com.sl.hx.shop.ui.mine.contacts.SettingContacts;
import com.sl.hx.shop.ui.mine.presnter.SettingPresenter;
import com.sl.hx.shop.utils.SPUtils;
import com.sl.hx.shop.utils.ToastUtil;
import com.trello.rxlifecycle2.android.ActivityEvent;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.Observable;

public class NickNameActivity extends BaseActivity {
    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.save)
    TextView save;
    @BindView(R.id.et_nameset)
    EditText etNameset;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_nickname;
    }

    @Override
    protected SettingPresenter createPresenter() {
        return new SettingPresenter(mContext);
    }

    @Override
    protected void initView() {

    }

    @Override
    protected void initData() {

    }


    @OnClick({R.id.iv_back, R.id.save})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.save:
                setNickname();
                break;
        }
    }

    //设置昵称
    private void setNickname() {
        if (TextUtils.isEmpty(etNameset.getText().toString().trim())){
            ToastUtil.showShort(mContext, "请输入修改后的昵称");
            return;
        }

        Observable observable = ApiFactory.getInstance().getNick(SPUtils.getInstance().getString(AppConfig.ID),etNameset.getText().toString().trim());
        HttpUtil.getInstance().toSubscribe(observable, new ProgressSubscribe<NickName>(mContext) {
            @Override
            protected void _onNext(NickName nickName) {
                ToastUtil.showShort(mContext,"修改成功");
                EventBus.getDefault().post(nickName.getNick());
                finish();
            }

            @Override
            protected void _onError(String message) {
            }
        }, true, this, ActivityEvent.DESTROY);
    }



}
